package grape.workflow.rest.common.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import grape.common.rest.form.BasePageForm;
import grape.common.rest.mvc.SuperController;
import org.flowable.ui.common.model.ResultListDataRepresentation;

/**
 * Created by yangwei
 * Created at 2020/6/23 14:40
 */
public class BaseController extends SuperController {


    public ObjectNode newObjectNode(){
        return new ObjectNode(JsonNodeFactory.instance);
    }

    /**
     * 兼容分页
     * @param pageForm
     * @param jsonNode
     */
    public void handlePage(BasePageForm pageForm, ObjectNode jsonNode) {
        jsonNode.put("page", pageForm.getCurrent() - 1);
        jsonNode.put("size", pageForm.getSize());
    }

    public IPage newPage(ResultListDataRepresentation resultListDataRepresentation, BasePageForm pageForm) {
        Page objectPage = new Page<>();
        objectPage.setCurrent(pageForm.getCurrent());
        objectPage.setSize(pageForm.getSize());
        objectPage.setTotal(resultListDataRepresentation.getTotal());
        return objectPage;
    }
}
